﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WeifenLuo.WinFormsUI.Base
{
    public partial class FormList : FormBaseDataGrid
    {
        public DataSet mds;
        private FormSearch frmSearch = null;

        public FormList(DataSet ads)
        {
            InitializeComponent();
            mds = ads;
        }

        protected override void BasicForm_Load(object sender, EventArgs e)
        {
            if (mds != null)
            {
                SetCurrentDataGrid(mdbg_dataGrid);
                mdbg_dataGrid.DataSource = mds.Tables[0];
                mRowManager = (CurrencyManager)mdbg_dataGrid.BindingContext[mds.Tables[0]];
                
                mdbg_dataGrid.Columns["CreatedUser"].HeaderText = "Created User";
                mdbg_dataGrid.Columns["UpdatedUser"].HeaderText = "Updated User";
                mdbg_dataGrid.Columns["CreatedDate"].HeaderText = "Created Date";
                mdbg_dataGrid.Columns["UpdatedDate"].HeaderText = "Updated Date";
                ConfigGrid();
                mdbg_dataGrid.AllowUserToAddRows = false;

            }
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                this.DialogResult = DialogResult.OK;
                return true;
            }
            return base.ProcessDialogKey(keyData);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                this.DialogResult = DialogResult.OK;
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }

        private void ConfigGrid()
        {
            switch (mds.Tables[0].TableName)
            {
                case "HRM_GLAccGroup_Header":
                    break;
                case "HRM_PersonnelHelthCheckBas":
                    break;

                case "HRM_PersonalProfile":
                    mdbg_dataGrid.Columns["Emp_NameVN"].Width = 200;
                    mdbg_dataGrid.Columns["Dept_Name"].Width = 200;
                    mdbg_dataGrid.Columns["Position_Name"].Width = 150;
                    break;

            }
        }

        public int GetCurrentRow()
        {
            return mdbg_dataGrid.CurrentCell.RowIndex;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (mdbg_dataGrid.Rows.Count < 1) return;
            DialogResult = DialogResult.OK;
        }

        protected override void mdbg_dataGrid_KeyUp(object sender, KeyEventArgs e)
        {
            //base.mdbg_dataGrid_KeyUp(sender, e);
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                this.DialogResult = DialogResult.OK;
            }
            //else if (e.Control && e.KeyCode == Keys.F)
            //{
            //    FormFind lfrm_Find = new FormFind();
            //    lfrm_Find.mfrmParentForm = this;
            //    lfrm_Find.ShowDialog();
            //}
        }

        private void mdbg_dataGrid_DoubleClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void mdbg_dataGrid_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            frmSearch = new FormSearch(this);
            //frmSearch.Owner = this;
            frmSearch.Show();
        }

        protected override void BasicForm_Closing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            base.BasicForm_Closing(sender, e);
            if (frmSearch != null) frmSearch.Close();
        }

    }
}
